package com.syncy.process.cron;

import java.util.Hashtable;

import it.sauronsoftware.cron4j.Scheduler;

public class CronProcess {

	private static final CronProcess instance = new CronProcess();
	
	private Scheduler scheduler = new Scheduler();
	
	private Hashtable<String, String> tasks = new Hashtable<String, String>();
	
	public static CronProcess getInstance() {
		return instance;
	}
	
	private CronProcess(){
		
	}
	
	public Scheduler getScheduler() {
		return this.scheduler;
	}
	
	public void addSchedule(String name, String schedulingPattern, Runnable task) {
		String id = this.scheduler.schedule(schedulingPattern, task);
		tasks.put(name, id);
		if(!this.scheduler.isStarted()) {
			this.scheduler.start();
		}
	}
	
	public void removeSchedule(String name){
		if(tasks.containsKey(name)) {
			String id = tasks.get(name);
			this.scheduler.deschedule(id);
			tasks.remove(name);
		}
	}
	
	
}
